package com.hue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hue.client.EmailFeignService;
import com.hue.entity.JwtUtils;
import com.hue.entity.MD5;
import com.hue.entity.R;
import com.hue.entity.UcenterMember;
import com.hue.entity.vo.RegisterVo;
import com.hue.mapper.UcenterMemberMapper;
import com.hue.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-07-05
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private EmailFeignService emailFeignService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /*
        登录，并使用jwt规则产生token返回
     */
    @Override
    public String login(UcenterMember ucenterMember) {
        //输入账号不能为空
        if (StringUtils.isEmpty(ucenterMember.getEmail())) {
            return null;
        }
        //输入密码不能为空
        if (StringUtils.isEmpty(ucenterMember.getPassword())) {
            return null;
        }
        //查询用户名密码是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", ucenterMember.getEmail());
        wrapper.eq("password", MD5.encrypt(ucenterMember.getPassword()));
        UcenterMember one = getOne(wrapper);
        System.out.println(one);
        //用户名密码错误
        if (one == null) {
            return null;
        }
        //账户被禁用
        if (one.getIsDisabled()) {
            return null;
        }
        //登录成功,产生token并返回
        String token = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return token;
    }

    /*
        注册
     */
    @Override
    public Map<String, String> register(RegisterVo registerVo) {
        Map<String, String> map = new HashMap<>();
        //判断输入是否合法
        if (StringUtils.isEmpty(registerVo.getNickname())) {
            map.put("error", "昵称不能为空");
            return map;
        }
        if (StringUtils.isEmpty(registerVo.getEmail())) {
            map.put("error", "邮箱不能为空");
            return map;
        }
        if (StringUtils.isEmpty(registerVo.getCode())) {
            map.put("error", "验证码不能为空");
            return map;
        }
        if (StringUtils.isEmpty(registerVo.getPassword())) {
            map.put("error", "密码不能为空");
            return map;
        }
        //判断验证码是否正确
        String check = redisTemplate.opsForValue().get(registerVo.getEmail());
        if (!registerVo.getCode().equals(check)) {    //验证码输入错误
            map.put("error", "验证码错误");
            return map;
        }
        //邮箱没有被使用过,注册用户
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo, ucenterMember);
        boolean b = save(ucenterMember);
        map.put("success", "注册成功");
        return map;
    }

    /*
        发送验证码
     */
    @Override
    public Map<String, String> sendCheckCode(String to) {
        Map<String, String> map = new HashMap<>();
        //判断该邮箱是否被使用
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", to);
        int count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            map.put("error","该邮箱已经被使用");
            return map;
        }
        //发送验证码
        R r = emailFeignService.sendEmail(to);
        System.out.println(r.getSuccess());
        if(!r.getSuccess()){
            map.put("error","验证码发送失败");
            return map;
        }
        return map;
    }
}
